import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 数据模型
 * @date: 2019-12-12
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryList as _queryList, queryModelDetail as _queryModelDetail, queryTablesList, createModel, queryFieldsList as _queryFieldsList, modifyModel, refreshField, createField, queryModelRelationList as _queryModelRelationList, modifyField, removeField as _removeField, createRelation as _createRelation, deleteRelation as _deleteRelation, saveRelationn } from "../services/flexModelService";
export default {
  namespace: 'flexModel',
  state: {
    dataSource: [],
    pagination: {},
    tables: []
  },
  effects: {
    // 获取列表
    queryList: /*#__PURE__*/_regeneratorRuntime.mark(function queryList(_ref, _ref2) {
      var params, call, put, response, dataSource, pagination;
      return _regeneratorRuntime.wrap(function queryList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              params = _ref.params;
              call = _ref2.call, put = _ref2.put;
              _context.t0 = getResponse;
              _context.next = 5;
              return call(_queryList, params);

            case 5:
              _context.t1 = _context.sent;
              response = (0, _context.t0)(_context.t1);
              dataSource = (response || {}).content || [];
              pagination = createPagination(response || {});

              if (!response) {
                _context.next = 12;
                break;
              }

              _context.next = 12;
              return put({
                type: 'updateState',
                payload: {
                  dataSource: dataSource,
                  pagination: pagination
                }
              });

            case 12:
              return _context.abrupt("return", {
                dataSource: dataSource,
                pagination: pagination
              });

            case 13:
            case "end":
              return _context.stop();
          }
        }
      }, queryList);
    }),
    // 查询模型详情
    queryModelDetail: /*#__PURE__*/_regeneratorRuntime.mark(function queryModelDetail(_ref3, _ref4) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function queryModelDetail$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              params = _ref3.params;
              call = _ref4.call;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(_queryModelDetail, params);

            case 5:
              _context2.t1 = _context2.sent;
              response = (0, _context2.t0)(_context2.t1);
              return _context2.abrupt("return", response || {});

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, queryModelDetail);
    }),
    fetchTablesList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchTablesList(_ref5, _ref6) {
      var params, call, put, response;
      return _regeneratorRuntime.wrap(function fetchTablesList$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              params = _ref5.params;
              call = _ref6.call, put = _ref6.put;
              _context3.t0 = getResponse;
              _context3.next = 5;
              return call(queryTablesList, params);

            case 5:
              _context3.t1 = _context3.sent;
              response = (0, _context3.t0)(_context3.t1);

              if (!response) {
                _context3.next = 10;
                break;
              }

              _context3.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  tables: response
                }
              });

            case 10:
              return _context3.abrupt("return", response);

            case 11:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchTablesList);
    }),
    saveModel: /*#__PURE__*/_regeneratorRuntime.mark(function saveModel(_ref7, _ref8) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function saveModel$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              params = _ref7.params;
              call = _ref8.call;
              _context4.t0 = getResponse;
              _context4.next = 5;
              return call(createModel, params);

            case 5:
              _context4.t1 = _context4.sent;
              response = (0, _context4.t0)(_context4.t1);
              return _context4.abrupt("return", response || {});

            case 8:
            case "end":
              return _context4.stop();
          }
        }
      }, saveModel);
    }),
    updateModel: /*#__PURE__*/_regeneratorRuntime.mark(function updateModel(_ref9, _ref10) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function updateModel$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              params = _ref9.params;
              call = _ref10.call;
              _context5.t0 = getResponse;
              _context5.next = 5;
              return call(modifyModel, params);

            case 5:
              _context5.t1 = _context5.sent;
              response = (0, _context5.t0)(_context5.t1);
              return _context5.abrupt("return", response);

            case 8:
            case "end":
              return _context5.stop();
          }
        }
      }, updateModel);
    }),
    // 获取字段列表
    queryFieldsList: /*#__PURE__*/_regeneratorRuntime.mark(function queryFieldsList(_ref11, _ref12) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function queryFieldsList$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              params = _ref11.params;
              call = _ref12.call;
              _context6.t0 = getResponse;
              _context6.next = 5;
              return call(_queryFieldsList, params);

            case 5:
              _context6.t1 = _context6.sent;
              response = (0, _context6.t0)(_context6.t1);
              return _context6.abrupt("return", {
                dataSource: response || []
              });

            case 8:
            case "end":
              return _context6.stop();
          }
        }
      }, queryFieldsList);
    }),
    // 字段初始化
    initFields: /*#__PURE__*/_regeneratorRuntime.mark(function initFields(_ref13, _ref14) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function initFields$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              params = _ref13.params;
              call = _ref14.call;
              _context7.t0 = getResponse;
              _context7.next = 5;
              return call(refreshField, params);

            case 5:
              _context7.t1 = _context7.sent;
              response = (0, _context7.t0)(_context7.t1);
              return _context7.abrupt("return", {
                dataSource: response || []
              });

            case 8:
            case "end":
              return _context7.stop();
          }
        }
      }, initFields);
    }),
    // 添加字段
    addField: /*#__PURE__*/_regeneratorRuntime.mark(function addField(_ref15, _ref16) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function addField$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              params = _ref15.params;
              call = _ref16.call;
              _context8.t0 = getResponse;
              _context8.next = 5;
              return call(createField, params);

            case 5:
              _context8.t1 = _context8.sent;
              response = (0, _context8.t0)(_context8.t1);
              return _context8.abrupt("return", response);

            case 8:
            case "end":
              return _context8.stop();
          }
        }
      }, addField);
    }),
    // 查询关联模型
    queryModelRelationList: /*#__PURE__*/_regeneratorRuntime.mark(function queryModelRelationList(_ref17, _ref18) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function queryModelRelationList$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              params = _ref17.params;
              call = _ref18.call;
              _context9.t0 = getResponse;
              _context9.next = 5;
              return call(_queryModelRelationList, params);

            case 5:
              _context9.t1 = _context9.sent;
              response = (0, _context9.t0)(_context9.t1);
              return _context9.abrupt("return", {
                dataSource: response || []
              });

            case 8:
            case "end":
              return _context9.stop();
          }
        }
      }, queryModelRelationList);
    }),
    // 修改字段
    saveField: /*#__PURE__*/_regeneratorRuntime.mark(function saveField(_ref19, _ref20) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function saveField$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              params = _ref19.params;
              call = _ref20.call;
              _context10.t0 = getResponse;
              _context10.next = 5;
              return call(modifyField, params);

            case 5:
              _context10.t1 = _context10.sent;
              response = (0, _context10.t0)(_context10.t1);
              return _context10.abrupt("return", response);

            case 8:
            case "end":
              return _context10.stop();
          }
        }
      }, saveField);
    }),
    // 刪除字段
    removeField: /*#__PURE__*/_regeneratorRuntime.mark(function removeField(_ref21, _ref22) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function removeField$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              params = _ref21.params;
              call = _ref22.call;
              _context11.t0 = getResponse;
              _context11.next = 5;
              return call(_removeField, params);

            case 5:
              _context11.t1 = _context11.sent;
              response = (0, _context11.t0)(_context11.t1);
              return _context11.abrupt("return", response);

            case 8:
            case "end":
              return _context11.stop();
          }
        }
      }, removeField);
    }),
    // 新建关系
    createRelation: /*#__PURE__*/_regeneratorRuntime.mark(function createRelation(_ref23, _ref24) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function createRelation$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              params = _ref23.params;
              call = _ref24.call;
              _context12.t0 = getResponse;
              _context12.next = 5;
              return call(_createRelation, params);

            case 5:
              _context12.t1 = _context12.sent;
              response = (0, _context12.t0)(_context12.t1);
              return _context12.abrupt("return", response);

            case 8:
            case "end":
              return _context12.stop();
          }
        }
      }, createRelation);
    }),
    // 删除关系
    deleteRelation: /*#__PURE__*/_regeneratorRuntime.mark(function deleteRelation(_ref25, _ref26) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function deleteRelation$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              params = _ref25.params;
              call = _ref26.call;
              _context13.t0 = getResponse;
              _context13.next = 5;
              return call(_deleteRelation, params);

            case 5:
              _context13.t1 = _context13.sent;
              response = (0, _context13.t0)(_context13.t1);
              return _context13.abrupt("return", response);

            case 8:
            case "end":
              return _context13.stop();
          }
        }
      }, deleteRelation);
    }),
    // 保存关系
    saveRelation: /*#__PURE__*/_regeneratorRuntime.mark(function saveRelation(_ref27, _ref28) {
      var params, call, response;
      return _regeneratorRuntime.wrap(function saveRelation$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              params = _ref27.params;
              call = _ref28.call;
              _context14.t0 = getResponse;
              _context14.next = 5;
              return call(saveRelationn, params);

            case 5:
              _context14.t1 = _context14.sent;
              response = (0, _context14.t0)(_context14.t1);
              return _context14.abrupt("return", response);

            case 8:
            case "end":
              return _context14.stop();
          }
        }
      }, saveRelation);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref29) {
      var payload = _ref29.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};